package com.jiwei.system.utils;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.jiwei.system.pojo.ResultPoJo;
import com.jiwei.system.vo.Login;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.util.Random;

/**
 * @title: SMSUtil
 * @Author 赖志宇
 * @Description TODO
 * @Date: 2020/8/14
 * @Version 1.0
 */
@Component
@ConfigurationProperties(prefix = "sms-config")
public class SMSUtil {

    private int APPID;
    private String APPKEY;
    private int TEMPLATEDID;
    private String SMSSIGN;
    private String candidateCode;
    private int codeMessageLength;

    public int getAPPID() {
        return APPID;
    }

    public void setAPPID(int APPID) {
        this.APPID = APPID;
    }

    public String getAPPKEY() {
        return APPKEY;
    }

    public void setAPPKEY(String APPKEY) {
        this.APPKEY = APPKEY;
    }

    public int getTEMPLATEDID() {
        return TEMPLATEDID;
    }

    public void setTEMPLATEDID(int TEMPLATEDID) {
        this.TEMPLATEDID = TEMPLATEDID;
    }

    public String getSMSSIGN() {
        return SMSSIGN;
    }

    public void setSMSSIGN(String SMSSIGN) {
        this.SMSSIGN = SMSSIGN;
    }

    public String getCandidateCode() {
        return candidateCode;
    }

    public void setCandidateCode(String candidateCode) {
        this.candidateCode = candidateCode;
    }

    public int getCodeMessageLength() {
        return codeMessageLength;
    }

    public void setCodeMessageLength(int codeMessageLength) {
        this.codeMessageLength = codeMessageLength;
    }

    /**
     * 发送短信，错误返回详情参考腾讯api文档
     * @param phoneNumber
     * @return 结果集 400表示手机号码错误，401是IO异常，402是网络服务出错，200表示成功发送
     * @throws HTTPException
     * @throws IOException
     */
    public ResultPoJo sendMessage(String phoneNumber, String []message)  {
        SmsSingleSender smsSingleSender = new SmsSingleSender(APPID,APPKEY);
        SmsSingleSenderResult smsSingleSenderResult;
        try {
           smsSingleSenderResult =  smsSingleSender.sendWithParam("86",phoneNumber,TEMPLATEDID,message,SMSSIGN,"","");
        } catch (HTTPException e) {
            e.printStackTrace();
            return new ResultPoJo("402","网络异常",null);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultPoJo("401","端口异常",null);
        }

        return smsSingleSenderResult.result ==0? new ResultPoJo("200","发送成功",null):new ResultPoJo("400","号码异常",null);
    }

    /**
     * 发送短信验证码
     * @param phoneNumber
     * @return 结果集 200就会包含login
     */
    public ResultPoJo sendSMSCode(String phoneNumber){
        String code = generateVerifyCode();
        ResultPoJo resultPoJo = sendMessage(phoneNumber,code.split(","));
        if(resultPoJo.getStatusCode().equals("200"))
        resultPoJo.setContent(new Login(phoneNumber,code));
        return resultPoJo;
    }

    /**
     * 用于生成随机的验证码
     * @return 6位数字验证码
     */
    private String generateVerifyCode(){
        Random random = new Random(System.currentTimeMillis());
        int resourceLength = candidateCode.length();
        StringBuilder code = new StringBuilder(codeMessageLength);
        for(int i=0;i<codeMessageLength;i++){
            code.append(candidateCode.charAt(random.nextInt(resourceLength-1)));
        }
        return code.toString();
    }
}
